<?php
/**
 * Created by PhpStorm.
 * User: winleung
 * Date: 2019-10-23
 * Time: 23:35
 */

namespace app\api\controller;


use app\api\service\PharmacyOrderService;
use think\Controller;
use think\Request;

class PharmacyOrder extends Controller
{
    private $pharmacyOrderService;

    public function __construct(Request $request, PharmacyOrderService $pharmacyOrderService)
    {
        parent::__construct($request);

        $this->pharmacyOrderService = $pharmacyOrderService;
    }

    /**
     * 更新订单关联表状态（1：待接单，2：已接单，在拣货，3，已通知物流取货, 4:拒绝接单）
     */
    public function putUpdatePharmacyOrderStatus()
    {
        $validateResult = $this->validate($this->request->post(), 'PharmacyOrder.updatePharmacyOrderStatus');
        if ($validateResult !== true) {
            throwBusinessException($validateResult);
        }

        $result = $this->pharmacyOrderService->updatePharmacyOrderStatus($this->request, $this->request->post());

        return jsonResult([], $result);
    }

    public function postControlDadaCallback()
    {
        $validateResult = $this->validate($this->request->post(), 'PharmacyOrder.controlDadaCallback');
        if ($validateResult !== true) {
            trace($validateResult, 'dadaCallback-error');
            throwBusinessException($validateResult);
        }
        $this->pharmacyOrderService->controlDadaCallback($this->request->post());

        return jsonResult();
    }

    public function putResendOrderToDada()
    {
        $validateResult = $this->validate($this->request->post(), 'PharmacyOrder.updatePharmacyOrderStatus');
        if ($validateResult !== true) {
            throwBusinessException($validateResult);
        }

        $result = $this->pharmacyOrderService->resendOrderToDada($this->request, $this->request->post());

        return jsonResult([], $result);
    }

    public function postSim()
    {
        $result = $this->pharmacyOrderService->sim($this->request->get("url"), $this->request->post());

        dump($result);
    }

    /**
     * 查询订单物流
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOrderLogistics(){

        $orderId = $this->request->get('order_id');
        if(empty($orderId)){
            throwBusinessException("order_id 不能为空");
        }
        $result = $this->pharmacyOrderService->fetchOrderLogistics($orderId);

        return jsonResult($result);
    }


}